﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "servermap.h"
#include "ServerConsole.h"
#include "AuthorHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CAuthorHandler
CAuthorHandler::CAuthorHandler(void)
: m_uAuthorOnline(0)
, m_uAuthorTotal(0)
, m_uAuthorLoad(0)
, m_uMasterCount(0)
, m_uMasterOnline(0)
, m_uMasterTotal(0)
{
}

CAuthorHandler::~CAuthorHandler(void)
{
}

void CAuthorHandler::OnLive(bool bStart)
{
    CStringFix strTemp = GetName();
    if (bStart)
    {
        strTemp += TF("Start---");

        CStrAddr strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_AUTHOR, 0, strAddr);
        strTemp.AppendFormat(TF("TCP Ext-Listen=[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        strTemp += TF("Stop");

        m_uAuthorOnline = 0;
        m_uAuthorTotal  = 0;
        m_uAuthorLoad   = 0;
        m_uMasterCount  = 0;
        m_uMasterOnline = 0;
        m_uMasterTotal  = 0;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CAuthorHandler::OnLink(Int nServerIndex, uintptr_t utData)
{
    CStringFix strTemp = GetName();
    if (nServerIndex == PAKT_CENTER)
    {
        strTemp += TF("TCP Int-Connect Center=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr  strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);
    }
    else if (nServerIndex == PAKT_AUTHORDB)
    {
        strTemp += TF("TCP Int-Connect AuthorDB=");

        if (utData != 0)
        {
            CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

            CStrAddr strAddr;
            GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_AUTHORDB, CServerConfig::CFG_LOAD_NONE, strAddr);
            strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

            GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
            strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);
        }
        else
        {
            strTemp += TF("---");
        }
    }
    else if (nServerIndex == PAKT_MASTER)
    {
        strTemp += TF("UDP Int-Listen Master=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr strAddr;
        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CAuthorHandler::OnUpdate(Int, uintptr_t)
{
}

void CAuthorHandler::OnSync(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_MASTER)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uMasterTotal  = pStatus->uTotal;
        m_uMasterOnline = pStatus->uOnline;
        m_uMasterCount  = pStatus->usLoad;
    }
    else
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uAuthorTotal  = pStatus->uTotal;
        m_uAuthorOnline = pStatus->uOnline;
        m_uAuthorLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
}

void CAuthorHandler::OnSync(Int nServerIndex, CStream& Stream)
{
    assert(nServerIndex == PAKT_MASTER);

    SVR_MASTER_MAP::SVR_PAIR Pair;
    while (Stream.IsEnd() == false)
    {
        Pair.Serialize(Stream);

        if (Pair.Value.usStatus == STATUSU_UNLINK)
        {
            RemoveServerData(nServerIndex, Pair.ullKey);
        }
        else if ((Pair.Value.usStatus == STATUSU_SYNC) || (Pair.Value.usStatus == STATUSU_LINK))
        {
            if (Pair.Value.usStatus == STATUSU_LINK)
            {
                AddServerData(nServerIndex, reinterpret_cast<uintptr_t>(&Pair));
            }
            else
            {
                UpdateServerData(nServerIndex, reinterpret_cast<uintptr_t>(&Pair));
            }
        }
    }
}

void CAuthorHandler::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    UNREFERENCED_PARAMETER(utData);
    if (nServerIndex == PAKT_CENTER)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Author:TCP Int-Connect Center Closed"));
    }
    else if (nServerIndex == PAKT_AUTHORDB)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Author:TCP Int-Connect AuthorDB Closed"));
    }
    else if (nServerIndex == PAKT_MASTER)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Author:UDP Int-Listen Master Closed"));
    }
}

void CAuthorHandler::UpdateServerInfo(void)
{
    CStringFix strTemp = GetName();
    strTemp.Format(TF("[%u/%u, Load=%d%%][Master:Count=%u, %u/%u]"),
                   m_uAuthorOnline, m_uAuthorTotal, m_uAuthorLoad,
                   m_uMasterCount, m_uMasterOnline, m_uMasterTotal);
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

PCXStr CAuthorHandler::GetName(void)
{
    return TF("Author:");
}




